<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Services\Admin\DictItemService;
use App\Http\Requests\Admin\DictItemRequest;

class DictItemController extends Controller
{

    public function options(string $dictCode)
    {

        $data = DictItemService::options($dictCode);

        return $this->success(data: $data);
    }

    /**
     * 显示资源列表。
     */
    public function index(Request $request, string $dictCode)
    {

        $params = $request->validate([
            'keywords'  => 'nullable|string',
            'pageNum'   => 'nullable|integer|min:1',
            'pageSize'  => 'nullable|integer|min:5',
        ]);

        $params['pageNum'] = $params['pageNum'] ?? 1;
        $params['pageSize'] = $params['pageSize'] ?? 10;

        $data = DictItemService::index($params, $dictCode);

        return $this->success(data: $data);
    }



    /**
     * 将新创建的资源存储到数据库。
     */
    public function store(DictItemRequest $request, string $dictCode)
    {
        $validated = $request->validated();

        $data = DictItemService::store($validated, $dictCode);

        return $this->success(data: $data);
    }


    /**
     * 显示指定资源的详细信息。
     */
    public function show(string $dictCode, string $id)
    {

        $data = DictItemService::show($dictCode, $id);

        return $this->success(data: $data);
    }

    /**
     * 更新指定资源。
     */
    public function update(DictItemRequest $request, string $dictCode, string $id)
    {
        $validated = $request->validated();

        $data = DictItemService::update($validated, $dictCode, $id);

        return $this->success(data: $data);
    }

    /**
     * 删除指定资源。
     */
    public function destroy(string $dictCode, string $id)
    {
        $data = DictItemService::destroy($dictCode, $id);

        return $this->success(data: $data);
    }
}
